package cn.jbolt.config;



import cn.jbolt.ai.resource.embedding.service.EmbeddingResourceService;
import cn.jbolt.ai.resource.function.service.AiFunctionResourceService;
import cn.jbolt.ai.resource.llm.service.AiResourceService;
import cn.jbolt.ai.resource.vdb.service.VDBResourceService;
import com.jboltai.config.JBoltAIConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

//自定义启动任务
@Component
public class ApplicationStartup {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationStartup.class);

    //ai资源
    private final AiResourceService aiResourceService;
    //Embedding资源
    private final EmbeddingResourceService embeddingResourceService;
    //vdb资源
    private final VDBResourceService vdbResourceService;

    //function资源
    private final AiFunctionResourceService functionResourceService;

    public ApplicationStartup(AiResourceService aiResourceService, EmbeddingResourceService embeddingResourceService,
                              VDBResourceService vdbResourceService, AiFunctionResourceService functionResourceService) {
        this.aiResourceService = aiResourceService;
        this.embeddingResourceService = embeddingResourceService;
        this.vdbResourceService = vdbResourceService;
        this.functionResourceService = functionResourceService;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void registerAllAiResources() {
        try {
            aiResourceService.registerAllResource();
            JBoltAIConfig.setHttpReadTimeout(600000);
        } catch (Exception e) {
            // 处理异常或记录日志
            logger.error("监听AiResource启动失败,失败原因:{}"+ e);
        }
    }
    @EventListener(ApplicationReadyEvent.class)
    public void registerAllEmbeddingResources() {
        try {
            embeddingResourceService.registerAllResource();
        } catch (Exception e) {
            // 处理异常或记录日志
            logger.error("监听EmbeddingResource启动失败,失败原因:{}"+ e);
        }
    }

    @EventListener(ApplicationReadyEvent.class)
    public void registerAllVDBResources() {
        try {
            vdbResourceService.registerAllResource();
        } catch (Exception e) {
            // 处理异常或记录日志
            logger.error("监听VDBResource启动失败,失败原因:{}"+ e);
        }
    }

    @EventListener(ApplicationReadyEvent.class)
    public void registerAllFunctionResources() {
        try {
            functionResourceService.registerAllResource();
        } catch (Exception e) {
            // 处理异常或记录日志
            logger.error("监听functionResource启动失败,失败原因:{}"+ e);
        }
    }
}
